package com.powernode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * @create: 2024/7/16
 * @author: gyhe10
 * @description: Redis配置类
 **/

@Configuration
public class RedisConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        // 创建RedisCacheConfiguration对象
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();

        redisCacheConfiguration
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()))// 设置缓存序列化方式为JSON
                .entryTtl(Duration.ofDays(7))// 统一设置缓存默认过期时间，7天
                .disableCachingNullValues();// 禁用缓存空值

        return redisCacheConfiguration;
    }
}
